#!/usr/bin/python
# -*- coding: utf-8 -*-

#为方便以后移植，参数中的 key 一律为字符串格式
import StringIO
import datetime
import logging

from google.appengine.ext import db
from google.appengine.api import memcache 
from google.appengine.api import users

import databasefunction
import commonfunction

import like.likedatabase as likedatabase



PAGE_SIZE = 20
YOYOKIT_URL = u'http://e-track.appspot.com'

RSS_CACHE_NAME_HOT_BLOG = "HotMicroblogRss"
RSS_CACHE_NAME_NEW_BLOG = "NewMicroblogRss"
RSS_CACHE_NAME_HOT_COLLECT = "HotCollectRss"
RSS_CACHE_NAME_NEW_COLLECT = "NewCollectRss"
RSS_CACHE_NAME_HOT_LIKE = "HotLikeRss"
RSS_CACHE_NAME_NEW_LIKE = "NewLikeRss"


CACHE_TIAN_TOP_NEWS = "TianYaTopNews"
CACHE_TIAN_HOT_NEWS = "TianYaHotNews"
RSS_CACHE_TIAN_TOP_NEWS = "TianYaTopNewsRss"
RSS_CACHE_TIAN_HOT_NEWS = "TianYaHotNewsRss"



def GetRssTitle(strTitle, TimeDelta):
    strRet = u'<?xml version="1.0" encoding="UTF-8" ?>\r\n<rss version="2.0">\r\n<channel>\r\n'
    strRet += u'<title>%s</title>\r\n' % ( strTitle )
    strRet += u'<link>' + YOYOKIT_URL + u'</link>\r\n<language>zh_CN</language>\r\n'
    strRet += u'<webMaster>yangyuqi@sina.com</webMaster>\r\n'
    strRet += u'<copyright>&amp;copy; 2009-2010 yoyokit. all rights reserved</copyright>\r\n'
    NowTime = datetime.datetime.now() + TimeDelta
    strRet += u'<pubDate>%s</pubDate>\r\n' % ( NowTime.strftime("%Y-%m-%d %H:%M:%S") )
    
    return strRet


def GetHotMicroblogRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_HOT_BLOG)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最热话题'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - hot blog'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        HotBlogs = databasefunction.GetHotMicroblogRecord(PAGE_SIZE)
        for Item in HotBlogs:
            output.write(u'<item>\r\n<category>Blog</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s...</title>\r\n' % Item.Content[0 : 20])
            output.write(u'<link>' + YOYOKIT_URL + u'/blog?tag=syshot&amp;item=%s</link>\r\n' % Item.key())
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_HOT_BLOG, output.getvalue())
        return output.getvalue()


def GetNewMicroblogRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_NEW_BLOG)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最新话题'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - new blog'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        NewBlogs, PrePageTag, NextPageTag = databasefunction.GetOneTableRecordAtPage('CMicroblogTable', PAGE_SIZE)
        for Item in NewBlogs:
            output.write(u'<item>\r\n<category>Blog</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s...</title>\r\n' % Item.Content[0 : 20])
            output.write(u'<link>' + YOYOKIT_URL + u'/blog?tag=sysnew&amp;item=%s</link>\r\n' % Item.key())
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_NEW_BLOG, output.getvalue())
        return output.getvalue()


def GetHotCollectRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_HOT_COLLECT)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最热收藏'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - hot collect'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        HotCollects = databasefunction.GetHotCollectRecord(PAGE_SIZE)
        for Item in HotCollects:
            output.write(u'<item>\r\n<category>Collect</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s</title>\r\n' % Item.Title)
            output.write(u'<link>' + YOYOKIT_URL + u'/collect?tag=syshot&amp;item=%s</link>\r\n' % Item.key())
            output.write(u'<description>%s</description>\r\n' % Item.SourceUrl.replace(u"&", u"&amp;"))
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_HOT_COLLECT, output.getvalue())
        return output.getvalue()


def GetNewCollectRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_NEW_COLLECT)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最新收藏'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - new collect'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        NewCollects, PrePageTag, NextPageTag = databasefunction.GetOneTableRecordAtPage('CCollectTable', PAGE_SIZE)
        for Item in NewCollects:
            output.write(u'<item>\r\n<category>Collect</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s</title>\r\n' % Item.Title)
            output.write(u'<link>' + YOYOKIT_URL + u'/collect?tag=sysnew&amp;item=%s</link>\r\n' % Item.key())
            output.write(u'<description>%s</description>\r\n' % Item.SourceUrl.replace(u"&", u"&amp;"))
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_NEW_COLLECT, output.getvalue())
        return output.getvalue()




def GetHotLikeRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_HOT_LIKE)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最受大家欢迎的'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - hot like'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        HotLikes = likedatabase.GetHotLikeRecord(PAGE_SIZE)
        for Item in HotLikes:
            output.write(u'<item>\r\n<category>Like</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s</title>\r\n' % Item.Title)
            output.write(u'<link>' + YOYOKIT_URL + u'/like?tag=syshot&amp;item=%s</link>\r\n' % Item.key())
            output.write(u'<description>%s</description>\r\n' % Item.SourceUrl.replace(u"&", u"&amp;"))
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_HOT_LIKE, output.getvalue())
        return output.getvalue()


def GetNewLikeRss(Language, TimeDelta):
    strRet = memcache.get(RSS_CACHE_NAME_NEW_LIKE)
    if strRet:
       return strRet
    else:
        strTitle = u'溜溜网 - 最新大家喜爱的'
        if Language != 'zh-cn':
            strTitle = u'yoyokit - new like'
        
        output = StringIO.StringIO()
        output.write(GetRssTitle(strTitle, TimeDelta))

        #Add item
        NewLikes, PrePageTag, NextPageTag = databasefunction.GetOneTableRecordAtPage('CLikeTable', PAGE_SIZE)
        for Item in NewLikes:
            output.write(u'<item>\r\n<category>Like</category>\r\n')
            output.write(u'<news_id>%s</news_id>' % Item.key())
            output.write(u'<title>%s</title>\r\n' % Item.Title)
            output.write(u'<link>' + YOYOKIT_URL + u'/like?tag=sysnew&amp;item=%s</link>\r\n' % Item.key())
            output.write(u'<description>%s</description>\r\n' % Item.SourceUrl.replace(u"&", u"&amp;"))
            
            Item.Date += TimeDelta
            output.write(u'<pubDate>%s</pubDate>\r\n' % Item.Date.strftime("%Y-%m-%d %H:%M:%S"))
            output.write(u'</item>\r\n')

        output.write(u'</channel>\r\n</rss>\r\n')
        
        memcache.set(RSS_CACHE_NAME_NEW_LIKE, output.getvalue())
        return output.getvalue()


def ResetRssCatch(RssName):
    if 'hotblog' == RssName:
        memcache.delete(RSS_CACHE_NAME_HOT_BLOG)
    elif 'newblog' == RssName:
        memcache.delete(RSS_CACHE_NAME_NEW_BLOG)
    elif 'hotcollect' == RssName:
        memcache.delete(RSS_CACHE_NAME_HOT_COLLECT)
    elif 'newcollect' == RssName:
        memcache.delete(RSS_CACHE_NAME_NEW_COLLECT)
    elif 'hotlike' == RssName:
        memcache.delete(RSS_CACHE_NAME_HOT_LIKE)
    elif 'newlike' == RssName:
        memcache.delete(RSS_CACHE_NAME_NEW_LIKE)
    else:
        return 0


    return 1



def SetTianYaTopRss(News, Language, TimeDelta):
    memcache.set(CACHE_TIAN_TOP_NEWS, News)
    
    strTitle = u'溜溜网 - 天涯头条'
    if Language != 'zh-cn':
        strTitle = u'yoyokit - Tianya top'
        
    output = StringIO.StringIO()
    output.write(GetRssTitle(strTitle, TimeDelta))

    for Url, Title in News.items():
        output.write(u'<item>\r\n<category>Tianya top</category>\r\n')
        output.write(u'<title>%s</title>\r\n' % Title)
        output.write(u'<link>%s</link>\r\n' % Url.replace(u"&", u"&amp;"))
            
        output.write(u'</item>\r\n')
        
    output.write(u'</channel>\r\n</rss>\r\n')
    memcache.set(RSS_CACHE_TIAN_TOP_NEWS, output.getvalue())



def SetTianYaHotRss(News, Language, TimeDelta):
    memcache.set(CACHE_TIAN_HOT_NEWS, News)
    
    strTitle = u'溜溜网 - 天涯热点'
    if Language != 'zh-cn':
        strTitle = u'yoyokit - Tianya hotspot'
        
    output = StringIO.StringIO()
    output.write(GetRssTitle(strTitle, TimeDelta))

    for Url, Title in News.items():
        output.write(u'<item>\r\n<category>Tianya hotspot</category>\r\n')
        output.write(u'<title>%s</title>\r\n' % Title)
        output.write(u'<link>%s</link>\r\n' % Url.replace(u"&", u"&amp;"))
            
        output.write(u'</item>\r\n')

        
    output.write(u'</channel>\r\n</rss>\r\n')    
    memcache.set(RSS_CACHE_TIAN_HOT_NEWS, output.getvalue())



def GetTianYaTopNews():
    return memcache.get(CACHE_TIAN_TOP_NEWS)

def GetTianYaTopNewsRSS():
    return memcache.get(RSS_CACHE_TIAN_TOP_NEWS)

def GetTianYaHotNews():
    return memcache.get(CACHE_TIAN_HOT_NEWS)

def GetTianYaHotNewsRSS():
    return memcache.get(RSS_CACHE_TIAN_HOT_NEWS)



